package com.nest.WorkNest.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lidroid.xutils.exception.HttpException;
import com.nest.WorkNest.R;
import com.nest.WorkNest.adapter.HolderAdapter;
import com.nest.WorkNest.api.XutilsPost;
import com.nest.WorkNest.entity.Order;
import com.nest.WorkNest.tools.Tool;
import com.nest.WorkNest.view.GridViewForScrollView;
import com.orhanobut.logger.Logger;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class DevelopmentDetailAct extends BaseActivity implements OnClickListener {
    @Bind(R.id.tv_needdetail_id)
    TextView tvneeddetailId;
    @Bind(R.id.tv_needdetail_name)
    TextView tvneeddetailName;
    @Bind(R.id.tv_needdetail_demo)
    TextView tvneeddetailDemo;
    @Bind(R.id.tv_needdetail_FAA)
    TextView tvneeddetailFAA;
    @Bind(R.id.tv_needdetail_AR)
    TextView tvneeddetailAR;
    @Bind(R.id.ll_needdetail_AR)
    LinearLayout llneeddetailAR;
    @Bind(R.id.tv_needdetail_CL)
    TextView tvneeddetailCL;
    @Bind(R.id.tv_needdetail_pack)
    TextView tvneeddetailPack;
    @Bind(R.id.tv_needdetail_Time)
    TextView tvneeddetailTime;
    @Bind(R.id.tv_needdetail_bigTime)
    TextView tvneeddetailBigTime;
    @Bind(R.id.tv_needdetail_FON)
    TextView tvneeddetailFON;
    @Bind(R.id.tv_needdetail_PL)
    TextView tvneeddetailPL;
    @Bind(R.id.tv_needdetail_PD)
    TextView tvneeddetailPD;
    @Bind(R.id.ll_needdetail_quotation)
    LinearLayout llneeddetailQuotation;
    @Bind(R.id.needdetail_items)
    GridViewForScrollView needdetailItems;
    @Bind(R.id.btn_needdetail_recommend)
    Button btnneeddetailRecommend;
    @Bind(R.id.tv_needdetail_ordertime)
    TextView tvneeddetailOrdertime;
    @Bind(R.id.btn_needdetail_submit)
    Button btn_needdetail_submit;
    @Bind(R.id.sv_needdetail)
    ScrollView svNeeddetail;
    private Context context = DevelopmentDetailAct.this;
    private String needId, QuoId, productName, imgUrl;
    private HashMap<String, String> data = new HashMap<>();
    private List<HashMap<String, String>> quotelist = new ArrayList<HashMap<String, String>>();
    private List<HashMap<String, String>> datalist = new ArrayList<HashMap<String, String>>();
    private HolderAdapter<HashMap<String, String>> mAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_needdetail);
        ButterKnife.bind(this);
        initBar();
        needId = getIntent().getStringExtra("needId");
        loadNeedId();
        svNeeddetail.scrollTo(0, 0);
        actionbar_side_tab_name.setVisibility(View.VISIBLE);
        actionbar_side_tab_name.setText("制版打样单详情");
        ll_iv_actionbar_tab_side_left.setVisibility(View.VISIBLE);
        tv_actionbar_tab_side_right.setVisibility(View.VISIBLE);
        tv_actionbar_tab_side_right.setText("状态记录");
        llneeddetailQuotation.setOnClickListener(this);
        tv_actionbar_tab_side_right.setOnClickListener(this);
        btn_needdetail_submit.setOnClickListener(this);
        if (!TextUtils.isEmpty(getIntent().getStringExtra("imgUrl"))) {
            imgUrl = getIntent().getStringExtra("imgUrl");
            Logger.d(imgUrl);

        }
        tabBack();
        initAdapter();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_needdetail_quotation:
                Intent intent = new Intent(context, QuotationDetailAct.class);
                intent.putExtra("quoteId", QuoId);
                startActivity(intent);
                break;
            case R.id.btn_needdetail_submit:
                loadSubmit();
                break;
            case R.id.tv_actionbar_tab_side_right:
                Intent intent1 = new Intent(context, StatusRecordAct.class);
                intent1.putExtra("bulkOrderId", needId);
                intent1.putExtra("productName", productName);
                intent1.putExtra("imgUrl", imgUrl);
                startActivity(intent1);
                break;
        }
    }

    public void loadSubmit() {
        XutilsPost xutilsPost = new XutilsPost();
        XutilsPost.IOAuthCallBack ioAuthCallBack = new XutilsPost.IOAuthCallBack() {
            @Override
            public void getIOAuthCallBack(String result) {
                Logger.json(result);
                if (Tool.getString(result, "resultCode").equals("0")) {
                    showToast("确认成功");
                    btn_needdetail_submit.setVisibility(View.GONE);
                }
            }

            @Override
            public void getDealException(HttpException httpException, String result) {

            }

            @Override
            public void getLoadingCallBack(long total, long current, boolean isUploading) {

            }
        };
        xutilsPost.doPostSubmit(context, needId, ioAuthCallBack);
    }

    public void loadNeedId() {
        XutilsPost xutilsPost = new XutilsPost();
        XutilsPost.IOAuthCallBack iOAuthCallBack = new XutilsPost.IOAuthCallBack() {
            @Override
            public void getIOAuthCallBack(String result) {
                Logger.json(result);
                if (Tool.getString(result, "resultCode").equals("0")) {
                    data = Tool.getMapByJsonObj(Tool.getString(result, "need"));
                    tvneeddetailId.setText(data.get(Order.needId));
                    tvneeddetailBigTime.setText(data.get(Order.bigTime));
                    btnneeddetailRecommend.setText(data.get(Order.statusNote));
                    if ("1".equals(data.get(Order.demo))) {
                        tvneeddetailDemo.setText("是");
                    } else if ("0".equals(data.get(Order.demo))) {
                        tvneeddetailDemo.setText("否");
                    } else {
                        tvneeddetailDemo.setText(data.get(Order.demo));
                    }
                    if ("1".equals(data.get(Order.FON))) {
                        tvneeddetailFON.setText("是");
                    } else if ("0".equals(data.get(Order.FON))) {
                        tvneeddetailFON.setText("否");
                    } else {
                        tvneeddetailFON.setText(data.get(Order.FON));
                    }
                    if ("10".equals(data.get(Order.FAA))) {
                        tvneeddetailFAA.setText("包工包料");
                    } else if ("11".equals(data.get(Order.FAA))) {
                        tvneeddetailFAA.setText("清加工");
                    } else if ("12".equals(data.get(Order.FAA))) {
                        tvneeddetailFAA.setText("我指定面料供应商");
                    } else {
                        tvneeddetailFAA.setText(data.get(Order.FAA));
                    }

                    if ("20".equals(data.get(Order.Cl))) {
                        tvneeddetailCL.setText("我提供");
                    } else if ("21".equals(data.get(Order.Cl))) {
                        tvneeddetailCL.setText("不需要");
                    } else if ("22".equals(data.get(Order.Cl))) {
                        tvneeddetailCL.setText("委托采购");
                    } else {
                        tvneeddetailCL.setText(data.get(Order.Cl));
                    }
                    if ("30".equals(data.get(Order.pack))) {
                        tvneeddetailPack.setText("我提供");
                    } else if ("31".equals(data.get(Order.pack))) {
                        tvneeddetailPack.setText("不需要");
                    } else if ("32".equals(data.get(Order.pack))) {
                        tvneeddetailPack.setText("委托采购");
                    } else {
                        tvneeddetailPack.setText(data.get(Order.pack));
                    }
                    if (data.get(Order.status).equals("1210")) {
                        btn_needdetail_submit.setVisibility(View.VISIBLE);
                    } else {
                        btn_needdetail_submit.setVisibility(View.GONE);
                    }
                    tvneeddetailTime.setText(data.get(Order.time));
                    tvneeddetailName.setText(data.get(Order.needname));
                    tvneeddetailPL.setText(data.get(Order.PL));
                    tvneeddetailOrdertime.setText(data.get(Order.orderTime));
                    tvneeddetailPD.setText(data.get(Order.PD));
                    productName = data.get(Order.needname);
                    JSONArray quoteArray = Tool.getJsonArray(result, "quoteList");
                    JSONArray imgArray = Tool.getJsonArray(result, "imageList");
                    quotelist = Tool.getListMapByJsonArray(quoteArray);
                    if (quotelist.size() != 0) {
                        QuoId = quotelist.get(0).get("quoteId");
                        if (!TextUtils.isEmpty(QuoId)) {
                            llneeddetailQuotation.setVisibility(View.VISIBLE);
                        }
                    }
                    datalist = Tool.getListMapByJsonArray(imgArray);
                    mAdapter.update(datalist);
                    if (data.get(Order.FAA).equals("19"))
                        tvneeddetailAR.setText(data.get(Order.AR));
                } else if (Tool.getString(result, "resultCode").equals("9001")) {
                    showToast("token已过期，请重新登录");
                    Intent intent = new Intent(context, LoginAct.class);
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void getDealException(HttpException httpException, String result) {

            }

            @Override
            public void getLoadingCallBack(long total, long current, boolean isUploading) {

            }
        };
        xutilsPost.doPostNeedDetail(context, needId, iOAuthCallBack);
    }

    public void initAdapter() {
        HolderAdapter.ViewCreator<HashMap<String, String>> creator = new HolderAdapter.ViewCreator<HashMap<String, String>>() {

            @Override
            public View createView(LayoutInflater inflater, int position,
                                   HashMap<String, String> map) {

                return LayoutInflater.from(context).inflate(R.layout.items_gridview_enquirydetail, null);
            }

            @Override
            public void updateView(View view, int position, HashMap<String, String> map) {
                ImageView pic = (ImageView) view.findViewById(R.id.iv_enquirydetail_pic);
                String imgUrl = datalist.get(position).get("url");
                if (!TextUtils.isEmpty(imgUrl)) {
                    if (!imgUrl.substring(0, 1).equals("h") && !imgUrl.substring(0, 1).equals("H")) {
                        imgUrl = "http://" + imgUrl;
                    }

                }
                Glide.with(context)
                        .load(imgUrl)
                        .centerCrop()
                        .placeholder(R.drawable.empty_photo)
                        .crossFade()
                        .into(pic);
            }
        };
        mAdapter = new HolderAdapter<>(this.getLayoutInflater(), creator);
        needdetailItems.setAdapter(mAdapter);
        mAdapter.update(datalist);
    }
//
//    protected void onResume() {
//        loadNeedId();
//        super.onResume();
//    }
}
